/**
 * 
 */
package com.xbang.attendance.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.util.StringUtil;
import org.apache.struts2.components.Bean;

import com.xbang.attendance.entity.system.MaintenanceStation;
import com.xbang.attendance.entity.system.Region;
import com.xbang.attendance.entity.system.Warehouse;
import com.xbang.attendance.service.WarehouseService;
import com.xbang.core.utils.CnToSpellUtils;
import com.xbang.core.utils.ReflectionUtils;

import enumeration.RegionType;

/**
 * @author dev.zhang
 * 
 *         维修站
 */
public class MaintenanceAnalyzeServiceImpl extends AbstractAnalyzeService {

	private Map<String, String> fieldMapping = new HashMap<String, String>();

	
	private WarehouseService warehouseService;

	
	@Override
	public void analyse(File file) throws Exception {

		
		POIFSFileSystem fs = null;
		HSSFWorkbook wb = null;
		fs = new POIFSFileSystem(new FileInputStream(file));
		wb = new HSSFWorkbook(fs);
		HSSFSheet sheet = wb.getSheetAt(0);

		setupExcelFieldMappings(sheet);

		int regionColumnIndex = getRegionIndex();
		int wareColumnIndx = getWareIndex();
		for (Row row : sheet) {
			if (row.getRowNum() == 0) {
				continue;
			}
			if (getCellValue(row.getCell(0)) == null || getCellValue(row.getCell(0)).toString().trim().isEmpty()) {
				continue;
			}
			Cell regionData = row.getCell(regionColumnIndex);
			Cell wareData = row.getCell(wareColumnIndx);
			Cell codeData = row.getCell(getCodeIndex());
			
			Warehouse warehouse = warehouseService.getWarehouseByCode(ReflectionUtils.parseString(getCellValue(wareData)));
			
			if (warehouse == null) {
				throw new Exception("仓储编码为：" + getCellValue(wareData) + "不存在！");
			}
			

			Region region = null;
		

			region = warehouseService.getRegionByName(getCellValue(regionData).toString());
			
			if (region == null) {
				region = new Region();
				region.setName(regionData.getStringCellValue());
				region.setNumber(CnToSpellUtils.getFullSpell(regionData.getStringCellValue()));
				region.setRegionType(RegionType.SGM);
				warehouseService.save(region);
			}
			
			MaintenanceStation station = warehouseService.getMaintenanceStationByCode(ReflectionUtils.parseString(getCellValue(codeData)));
			if(station == null)
				station = new MaintenanceStation();
			for (Cell cell : row) {
				if(cell.getColumnIndex() == getRegionIndex() || cell.getColumnIndex() == getWareIndex())
					continue;
				if(getCellValue(cell) != null){
					ReflectionUtils.setFeildByApache(station,getPropertyName(cell.getColumnIndex(), fieldMapping),ReflectionUtils.parseString(getCellValue(cell)));
				}	
			}
			station.setRegion(region);
			station.setMainWarehouse(warehouse);
		
			warehouseService.saveOrUpdate(station);
			//System.out.println(station);
		}
	}
	
	

	private int getRegionIndex() {
		return excelFieldMapping.get("区域");

	}
	
	private int getCodeIndex()
	{
		return excelFieldMapping.get("编码");
	}
	private int getWareIndex() {
		return excelFieldMapping.get("仓储商编码");
	}

	public WarehouseService getWarehouseService() {
		return warehouseService;
	}

	public void setWarehouseService(WarehouseService warehouseService) {
		this.warehouseService = warehouseService;
	}



	public Map<String, String> getFieldMapping() {
		return fieldMapping;
	}



	public void setFieldMapping(Map<String, String> fieldMapping) {
		this.fieldMapping = fieldMapping;
	}


}
